package vo;

import file.CfgOperator;
import common.Singleton;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Attr包含每种属性的候选词和目录
 *
 * @author 柴家琪
 */
public class Attr {
    private String text;
    // 隶属于第几种属性
    private int index;
    private ArrayList<Attr> folders = new ArrayList<>(); // 目录
    private ArrayList<String> candidates = new ArrayList<>(); // 候选词
    private Attr boss;
    public boolean changed = false;

    private CfgOperator cfgOperator;
    private Attr() {
        cfgOperator = Singleton.cfgOperator();
    }

    public Attr(int index) {
        this();
        this.index = index;
    }

    public Attr(String text, int index) {
        this();
        this.text = text;
        this.index = index;
    }

    public void addFolder(Attr attr, boolean recreate) {
        if (attr != null) {
            folders.add(attr);
            attr.setBoss(this);
            cfgOperator.config_recreate = recreate;
            changed = recreate;
        }
    }

    public void addNew(String str, boolean recreate) {
        if (!str.equals("")) {
            candidates.add(str);
            cfgOperator.config_recreate = recreate;
            changed = recreate;
        }
    }

    public void delete(String str) {
        candidates.remove(str);
        cfgOperator.config_recreate = true;
        changed = true;
    }

    public void delete(Attr attr) {
        folders.remove(attr);
        cfgOperator.config_recreate = true;
        changed = true;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Attr[] getFolders() {
        Attr a[] = new Attr[folders.size()];
        Iterator<Attr> ia = folders.iterator();
        int i = 0;
        while (ia.hasNext()) {
            a[i++] = ia.next();
        }
        return a;
    }

    // public ArrayList<Attr> getFolders(){
    // return folders;
    // }
    public String[] getArray() {
        String s[] = new String[candidates.size()];
        Iterator<String> is = candidates.iterator();
        int i = 0;
        while (is.hasNext()) {
            s[i++] = is.next();
        }
        return s;
    }

    public void setCandidate(ArrayList<String> list) {
        this.candidates = list;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public ArrayList<String> getCandidate() {
        return candidates;
    }

    public Attr getBoss() {
        return boss;
    }

    private void setBoss(Attr boss) {
        this.boss = boss;
    }

    public boolean hasBoss() {
        return boss != null;
    }

    public String getPath() {
        String path = getText();
        if (hasBoss())
            path = getBoss().getPath() + "-" + path;
        return path;
    }

    public boolean containsFolder(String name) {
        for (Attr folder : folders) {
            if (folder.getText().equals(name))
                return true;
        }
        return false;
    }
}
